<?php
/**
 * Created by PhpStorm.
 * User: moon
 * Date: 2017/2/22
 * Time: 11:47
 * curl 请求发送核心类  -- 用来静态调用
 */
/**
 * Class Mongocurl
 * @package app\traits
 * 这个是MONGO 日志专用的CURL 其它方法 不要使用了
 */
namespace application\depend;
defined('API_CONNECT_TIMEOUT') or define('API_CONNECT_TIMEOUT', 3);
defined('API_RETURN_TIMEOUT') or define('API_RETURN_TIMEOUT', 35);

class AppCurl
{


    private  $opt = [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_CONNECTTIMEOUT => API_CONNECT_TIMEOUT, // timeout on connect
        CURLOPT_TIMEOUT => API_RETURN_TIMEOUT
    ]; // timeout on response

    public $http_code = "";
    public $curl_time = "";
    public $curl_errno = "";
    public $curl_errinfo = "";
    /**
     * @param bool $flag   true 就是调L2 接口  默认是L3接口
     * @return array|mixed
     */
    private  function exec()
    {
        $ch = curl_init();
        curl_setopt_array($ch, $this->opt);
        $data = curl_exec($ch);
        $errno = curl_errno($ch);
        $errinfo = curl_error($ch);
        $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        $curlTime = curl_getinfo($ch,CURLINFO_TOTAL_TIME );
        $this->http_code = $httpCode;
        $this->curl_time = $curlTime;
        $this->curl_errno = $errno;
        $this->curl_errinfo = $errinfo;
        unset($errno);
        unset($errinfo);
        unset($httpCode);
        unset($curlTime);
        curl_close($ch);
        Applog::debugLogs([
            'post-url' => $this->opt[CURLOPT_URL],
            'post-data' => $this->opt[CURLOPT_POSTFIELDS],
            'response-data' => $data,
            'http-code' => $this->http_code,
            'spend-time' => $this->curl_time,
            'error' => $this->curl_errno,
            'error_info' => $this->curl_errinfo
        ],__FILE__,__LINE__,'curl-log');
        return $data;
    }//end function

    /**
     * post 方法
     *
     * @param string $url
     * @param string $data
     * @param string $config
     * @param string $uploadFile  false 没有上传文件   true 有   默认为无
     * @return multitype:string mixed
     */
    public  function curlPost($url = '', $data = '', $uploadFile = false,$config = '')
    {

//        echo $url;exit;
        $this->opt[CURLOPT_POST] = 1;
        $this->opt[CURLOPT_URL] = $url;
        if (is_array($data) && !empty($data)) {
            if($uploadFile) {   //有上传文件 就不要用那个方法
                $senddata = $data;
            }else{
                $senddata = http_build_query($data);
            }
            $this->opt[CURLOPT_POSTFIELDS] = $senddata;
            unset($senddata);  //释放内存
        } else
            if (is_string($data) && !empty($data)) {
                $this->opt[CURLOPT_HTTPHEADER] = [
                    'Content-Type: application/json; charset=utf-8',
                    'Content-Length: ' . strlen($data)
                ];
                $this->opt[CURLOPT_POSTFIELDS] = $data;
            } else {
                $this->opt[CURLOPT_POSTFIELDS] = '';
            }

        if (empty($data)) {
            $this->opt[CURLOPT_POSTFIELDS] = '';
        }
        if (is_array($config)) {
            foreach($config as $k => $v){
                $this->opt[$k] = $v;
            }
        }
        return $this->exec();
    }
    // end function

    /**
     * get 方法
     *
     * @param string $url
     * @param string $data
     * @param string $config
     */
    public  function curlGet($url = '', $data = '', $config = '')
    {
        if (is_array($data)) {
            $url = $url . '?' . http_build_query($data);
        } else if (is_string($data) && !empty($data)) {
            $url = $url . '?' . $data;
        }
        $this->opt[CURLOPT_URL] = $url;
        if (is_array($config)) {
            array_merge($this->opt, $config);
        }
        return $this->exec();
    } // end function


}